import system_department from "~/apis/api/system_department";
import BaseCacheData from "~/store/base/baseCacheData";

class DepartmentCache extends BaseCacheData {
  departments: Array<Department> = [];
  departmentSelectSource: Array<ForeignKeyModel> = [];
  departmentTreeSelectSource: Array<TreeForeignKeyModel> = [];
  noSelectNode: any = { value: "0", text: "不设置" };

  constructor() {
    super();
    this.update();
  }

  update() {
    super.update();
    system_department
      .getAll()
      .then((res) => {
        this.departments = res.data;
        this.departmentSelectSource = super.toForeignKeyDataList(
          res.data,
          "name",
          "id"
        );
        this.departmentTreeSelectSource = super.toTree(
          res.data,
          "id",
          "parentId",
          "name"
        );
        this.departmentTreeSelectSource = [
          this.noSelectNode,
          ...this.departmentTreeSelectSource,
        ];
        this.departmentSelectSource = [
          this.noSelectNode,
          ...this.departmentSelectSource,
        ];
        return this;
      })
      .catch((err) => {
        return null;
      });
  }
}

export default new DepartmentCache();
